package com.haoshuang.zhouzhoubang.ui.page.mine.invite.create

import android.content.Context
import android.os.Bundle
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityCreateTeamMemberBinding
import com.haoshuang.zhouzhoubang.ui.activity.AddPhoneNumberBean
import com.haoshuang.zhouzhoubang.ui.activity.FriendInvitationActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.invite.skill.ChoosePassSkillListActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 * 创建门店成员
 */
class CreateTeamMemberActivity :
    BaseVMActivity<CreateTeamMemberViewModel, ActivityCreateTeamMemberBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<CreateTeamMemberActivity>()
        }
    }

    private var id1s = ""
    private var id2s = ""
    private var id3s = ""
    private var chooseNum = 0
    private val data = arrayListOf(AddPhoneNumberBean())
    private val adapter = CreateTeamMemberAdapter(data,  ::minus)

    private fun add(phone: String, position: Int) {
        if (phone.isEmpty()) {
            toast("请填写手机号")
            return
        }
        if (phone.length != 11 || !phone.startsWith("1")) {
            toast("请填写正确的手机号")
            return
        }
        data[position] = AddPhoneNumberBean(phone)
        data.add(AddPhoneNumberBean())
        adapter.notifyDataSetChanged()
    }

    private fun minus(position: Int) {
        data.removeAt(position)
        adapter.notifyDataSetChanged()
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            titleBar.title = "队员创建"
            titleBar.leftImageView().click { finish() }

            recyclerView.vertical().adapter = adapter

            llChooseSkill.clickWithTrigger {
                startPageForResult<ChoosePassSkillListActivity> { code, data ->
                    if (code == 200 && data != null) {
                        id1s = data.getString("id1s")
                        id2s = data.getString("id2s")
                        id3s = data.getString("id3s")
                        chooseNum = data.getIntExtra("chooseNum", 0)
                        if (chooseNum == 0) {
                            tvChooseSkillNum.text = ""
                        } else {
                            tvChooseSkillNum.text = "已选${chooseNum}项"
                        }
                    }
                }
            }

            tvShare.clickWithTrigger {
                // 验证
                if (chooseNum == 0) {
                    toast("请选择技能")
                    return@clickWithTrigger
                }

                // 验证手机号
                if (data.none { it.phoneNumber.isNotEmpty() }) {
                    toast("请填写至少一个手机号")
                    return@clickWithTrigger
                }
                data.filter { it.phoneNumber.isNotEmpty() }.forEach {
                    if (it.phoneNumber.length != 11 || !it.phoneNumber.startsWith("1")){
                        toast("请输入正确的手机号")
                        return@clickWithTrigger
                    }
                }
                // 提交
                vm.inviteTeamMember(data.filter { it.phoneNumber.isNotEmpty() }.joinToString(",") { it.phoneNumber }, id1s, id2s, id3s)
            }
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.inviteTeamMemberData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                startPage<FriendInvitationActivity>()
                finish()
            }
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarColor(R.color.white)
        }
    }

}